package com.yuzhi.master.drainCorrelation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.common.util.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import com.yuzhi.master.drainCorrelation.domain.DrainCorrelation;
import com.yuzhi.master.drainCorrelation.domain.bo.DrainCorrelationBo;
import com.yuzhi.master.drainCorrelation.domain.request.DrainCorrelationReq;
import com.yuzhi.master.drainCorrelation.domain.vo.DrainCorrelationVo;
import com.yuzhi.master.drainCorrelation.mapper.DrainCorrelationMapper;
import com.yuzhi.master.drainCorrelation.service.IDrainCorrelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.List;

/**
 * 排水系统关联表Service业务层处理
 *
 * @author auto code generator
 * @date 2025-08-13
 */
@Validated
@RequiredArgsConstructor
@Service
public class DrainCorrelationServiceImpl implements IDrainCorrelationService {

    private final DrainCorrelationMapper baseMapper;

    /**
     * 查询排水系统关联表
     */
    @Override
    public DrainCorrelationVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询排水系统关联表列表
     */
    @Override
    public PageDataInfo<DrainCorrelationVo> queryPageList(DrainCorrelationReq request) {
        Page<DrainCorrelationVo> page = baseMapper.selectPageList(
            new Page<>(request.getPageQuery().getPageNum(), request.getPageQuery().getPageSize()),
            buildQueryWrapper(request)
        );
        return PageDataInfo.build(page);
    }

    /**
     * 查询排水系统关联表列表
     */
    @Override
    public List<DrainCorrelationVo> queryList(DrainCorrelationReq request) {
        return baseMapper.selectVoList(buildQueryWrapper(request));
    }

    private LambdaQueryWrapper<DrainCorrelation> buildQueryWrapper(DrainCorrelationReq request) {
        LambdaQueryWrapper<DrainCorrelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(request.getId()), DrainCorrelation::getId, request.getId());
        lqw.eq(StringUtils.isNotBlank(request.getStcd()), DrainCorrelation::getStcd, request.getStcd());
        lqw.eq(StringUtils.isNotBlank(request.getDraiPipeId()), DrainCorrelation::getDraiPipeId, request.getDraiPipeId());
        lqw.eq(StringUtils.isNotBlank(request.getManholeId()), DrainCorrelation::getManholeId, request.getManholeId());
        lqw.eq(StringUtils.isNotBlank(request.getDraiPumpId()), DrainCorrelation::getDraiPumpId, request.getDraiPumpId());
        lqw.eq(StringUtils.isNotBlank(request.getTrunkmainId()), DrainCorrelation::getTrunkmainId, request.getTrunkmainId());
        return lqw;
    }

    /**
     * 新增排水系统关联表
     */
    @Override
    public Boolean insertByBo(DrainCorrelationBo bo) {
        DrainCorrelation add = BeanUtil.toBean(bo, DrainCorrelation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改排水系统关联表
     */
    @Override
    public Boolean updateByBo(DrainCorrelationBo bo) {
        DrainCorrelation update = BeanUtil.toBean(bo, DrainCorrelation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DrainCorrelation entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 根据id删除排水系统关联表信息
     */
    @Override
    public Boolean deleteById(String id, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 校验并批量删除排水系统关联表信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据测站ID查询关联信息
     */
    @Override
    public DrainCorrelationVo queryByStcd(String stcd) {
        LambdaQueryWrapper<DrainCorrelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(DrainCorrelation::getStcd, stcd);
        lqw.eq(DrainCorrelation::getDeleted,0);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 根据管道ID查询关联信息
     */
    @Override
    public DrainCorrelationVo queryByDraiPipeId(String draiPipeId) {
        LambdaQueryWrapper<DrainCorrelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(DrainCorrelation::getDraiPipeId, draiPipeId);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 根据管井ID查询关联信息
     */
    @Override
    public DrainCorrelationVo queryByManholeId(String manholeId) {
        LambdaQueryWrapper<DrainCorrelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(DrainCorrelation::getManholeId, manholeId);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 根据泵站ID查询关联信息
     */
    @Override
    public DrainCorrelationVo queryByDraiPumpId(String draiPumpId) {
        LambdaQueryWrapper<DrainCorrelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(DrainCorrelation::getDraiPumpId, draiPumpId);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 根据干管ID查询关联信息
     */
    @Override
    public List<DrainCorrelationVo> queryByTrunkmainId(String trunkmainId) {
        LambdaQueryWrapper<DrainCorrelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(DrainCorrelation::getTrunkmainId, trunkmainId);
        return baseMapper.selectVoList(lqw);
    }
}
